package com.mkx.domain.entity;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 租户用户实体
 * 表示用户在特定租户中的关联信息
 */
@Getter
@Builder
public class TenantUser {
    
    /**
     * ID
     */
    private final String id;
    
    /**
     * 租户ID
     */
    private final String tenantId;
    
    /**
     * 用户ID
     */
    private final String userId;
    
    /**
     * 租户内角色ID
     */
    @Setter
    private String roleId;
    
    /**
     * 创建时间
     */
    private final LocalDateTime createdTime;
    
    /**
     * 更新时间
     */
    private LocalDateTime updatedTime;
    
    /**
     * 构造函数
     */
    public TenantUser(String id, String tenantId, String userId, String roleId, LocalDateTime createdTime, LocalDateTime updatedTime) {
        this.id = id != null ? id : UUID.randomUUID().toString();
        this.tenantId = tenantId;
        this.userId = userId;
        this.roleId = roleId;
        this.createdTime = createdTime != null ? createdTime : LocalDateTime.now();
        this.updatedTime = updatedTime != null ? updatedTime : LocalDateTime.now();
    }
    
    /**
     * 更新用户在租户中的角色
     */
    public void updateRole(String roleId) {
        this.roleId = roleId;
        this.updatedTime = LocalDateTime.now();
    }
    
    /**
     * 检查是否属于指定租户
     */
    public boolean belongsToTenant(String tenantId) {
        return this.tenantId.equals(tenantId);
    }
    
    /**
     * 检查是否为指定用户
     */
    public boolean isUser(String userId) {
        return this.userId.equals(userId);
    }
}